package com.masterclient.app.widget;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.masterclient.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.util.TypedValue.COMPLEX_UNIT_SP;

/**
 * @auther hjian
 * @date 2020/6/10.
 */
public class EditRemarksDialog extends Dialog {

    @BindView(R.id.dialog_remarks_title)
    TextView dialogRemarksTitle;
    @BindView(R.id.dialog_remarks_edit)
    EditText dialogRemarksEdit;
    @BindView(R.id.dialog_remarks_hint)
    TextView dialogRemarksHint;
    @BindView(R.id.dialog_remarks_cancel)
    TextView dialogRemarksCancel;
    @BindView(R.id.dialog_remarks_confirm)
    TextView dialogRemarksConfirm;
    private Activity context;

    @OnClick({R.id.dialog_remarks_cancel, R.id.dialog_remarks_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_remarks_cancel:
                dismiss();
                break;
            case R.id.dialog_remarks_confirm:
                String content = dialogRemarksEdit.getText().toString();
                if (!TextUtils.isEmpty(content.trim())) {
                    saveRemarksCallback.onSaveRemarks(content);
                    dismiss();
                }else {
                    ToastUtil.showToast("填写不能为空");
                }
                break;
        }
    }

    public interface SaveRemarksCallback {
        void onSaveRemarks(String remarks);
    }

    private SaveRemarksCallback saveRemarksCallback;
    private String title, editHint, textHint, content;
    private int type = 0;

    public EditRemarksDialog(@NonNull Activity context, String title, SaveRemarksCallback saveRemarksCallback) {
        super(context, R.style.MyDialog);
        this.context = context;
        this.saveRemarksCallback = saveRemarksCallback;
        this.title = title;
    }
    public EditRemarksDialog( Activity context, String content, String title, String editHint,
            int type, String textHint, SaveRemarksCallback saveRemarksCallback) {
       this(context,title,saveRemarksCallback);
       this.editHint = editHint;
       this.textHint = textHint;
       this.type = type;
       this.content = content;
    }


    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        windowparams.width = ScreenUtils.getScreenWidth();
        windowparams.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(R.color.transparent);
        window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.setAttributes(windowparams);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_edit_remarks);
        ButterKnife.bind(this);
        setCanceledOnTouchOutside(false);
        if (!TextUtils.isEmpty(content)){
            dialogRemarksEdit.setText(content);
            dialogRemarksEdit.setSelection(content.length());
        }
        if (type == 0) {
            dialogRemarksTitle.setVisibility(TextUtils.isEmpty(title)?View.GONE:View.VISIBLE);
            dialogRemarksTitle.setText("该用户所属会员为：" + title);
        }else {
            dialogRemarksTitle.setText(title);
            dialogRemarksEdit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(18){}});
            dialogRemarksTitle.setTextSize(COMPLEX_UNIT_SP,15);
            dialogRemarksEdit.setHint(editHint);
            dialogRemarksHint.setText(textHint);
        }
    }

    @Override
    public void show() {
        super.show();
    }
}
